package com.atgm.mall.gmmailmember.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.vo.login.WeiboUser;
import com.atgm.common.vo.member.MemberInfoVo;
import com.atgm.mall.gmmailmember.vo.req.MemberLoginVo;
import com.atgm.mall.gmmailmember.vo.req.MemberRegistVo;
import com.atgm.mall.gmmailmember.vo.resp.MemberLoginRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.atgm.mall.gmmailmember.entity.MemberEntity;
import com.atgm.mall.gmmailmember.service.MemberService;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.R;

import javax.validation.Valid;


/**
 * 会员
 *
 * @author pdx
 * @email 945097967@qq.com
 * @date 2021-08-20 10:00:59
 */
@RestController
@RequestMapping("gmmallmember/member")
@Validated
public class MemberController {

    @Autowired
    private MemberService memberService;

    @PostMapping("/weiboLogin")
    public R handlerWeiboLogin(@RequestBody WeiboUser weiboUser) {
        String memberId = memberService.weiboLogin(weiboUser);
        return R.ok().setData(memberId);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping(value = "/login")
    public R login(@RequestBody MemberLoginVo vo) {
        MemberLoginRespVo memberLoginRespVo = memberService.login(vo);
        if (memberLoginRespVo != null) {
            return R.ok().setData(memberLoginRespVo);
        } else {
            return R.error(BizCodeEnume.USER_LOGIN.getCode(), "登录失败");
        }
    }

    @PostMapping("/register")
    public R register(@Valid @RequestBody MemberRegistVo vo) {
        memberService.regist(vo);
        return R.ok();
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);
        return R.ok().setData(member);
    }

    /**
     * 获取会员信息
     * @param memberIds
     * @return
     */
    @GetMapping("/infos")
    public R getMemberInfo(@RequestParam("ids") List<Long> memberIds) {
        List<MemberInfoVo> list = memberService.queryMemberInfo(memberIds);
        return R.ok().setData(list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
